package com.atguigu.tingshu.search.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.DefaultManagedAwareThreadFactory;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ClassName: ThreadPoolConfig
 * Package: com.atguigu.tingshu.search.config
 * Description:配置线程池
 */
@Configuration
public class ThreadPoolConfig {
    /**
     * 实例化线程池
     * @return
     */
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //获取cpu核心数
        int processors = Runtime.getRuntime().availableProcessors();
        //创建线程池
        return new ThreadPoolExecutor(
                processors,
                processors*2,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(100),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
                );
    }
}
